//
//  CMLoginViewController.m
//  BlackBambooCM-OC
//
//  Created by 宜凯门-DKang on 2018/9/26.
//  Copyright © 2018年 ucanmax. All rights reserved.
//

#import "CMLoginViewController.h"

@interface CMLoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *userNameTf;
@property (weak, nonatomic) IBOutlet UITextField *passwordTf;
@property (weak, nonatomic) IBOutlet UITextField *codeTf;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (retain, nonatomic) NSString *userName;
@property (retain, nonatomic) NSString *password;
@property (retain, nonatomic) NSString *code;
@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (assign, nonatomic) CGFloat viewX;
@property (assign, nonatomic) CGFloat viewY;
@property (assign, nonatomic) CGFloat viewWidth;
@property (assign, nonatomic) CGFloat viewHeight;
//键盘
@property (assign, nonatomic) CGFloat keyboardHeight;

@end

@implementation CMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)initView {
    self.viewX = self.inputView.frame.origin.x;
    self.viewY = UCMScreenHeight-self.inputView.frame.size.height-50;
    self.viewWidth = self.inputView.frame.size.width;
    self.viewHeight = self.inputView.frame.size.height;
    
    [self addNavigationItemWithImgs:@[@"backWhite"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
    [self getCode];
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.layer.cornerRadius = 8;
    self.codeButton.layer.masksToBounds = YES;
    self.codeButton.layer.cornerRadius = 8;
    
    //添加输入监控
    [self.userNameTf addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventValueChanged];
    [self.passwordTf addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventValueChanged];
    [self.codeTf addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventValueChanged];
    [self keyboard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender {
    [self getCode];
}
- (void) getCode {
    NSString *url = [[WYNetwork sharedWYNetwork] getIdentifyingCodeUrl];
    [self.codeButton sd_setImageWithURL:[NSURL URLWithString:url] forState:UIControlStateNormal];
}
- (void)textChange:(UITextField *)tf {
    switch (tf.tag) {
        case 100:{
            self.userName = tf.text;
        }
            break;
        case 101:{
            self.password = tf.text;
        }
            break;
        case 102:{
            self.code = tf.text;
        }
            break;
        default:
            break;
    }
}
- (IBAction)loginClick:(id)sender {
    NSLog(@"userName:%@,password:%@,code:%@",self.userName,self.password,self.code);
    if(!self.userName){
        [MBProgressHUD showTips:@"请输入用户名" toView:self.view];
        return;
    }
    if (!self.password) {
        [MBProgressHUD showTips:@"请输入密码" toView:self.view];
        return;
    }
    if (!self.code) {
        [MBProgressHUD showTips:@"请输入验证码" toView:self.view];
        return;
    }
    [[BBSUser sharedInstance] loginWith:self.userName password:self.password captcha:self.code success:^(id  _Nonnull responseObject) {
        
    } failure:^(NSString * _Nonnull error) {
        
    }];
}
//键盘处理
- (void)keyboard {
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    self.keyboardHeight = keyboardRect.size.height;
    [self updateInputView];
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    [UIView animateWithDuration:1 animations:^{
        self.inputView.frame = CGRectMake(_viewX, _viewY, _viewWidth, _viewHeight);
    }];
}
- (void)updateInputView {
    [UIView animateWithDuration:1 animations:^{
        self.inputView.frame = CGRectMake(_viewX, UCMScreenHeight-self.keyboardHeight-_viewHeight, _viewWidth, _viewHeight);
    }];
}
@end
